package cn.wolf.wechat.controller;


import cn.hutool.crypto.SecureUtil;
import cn.wolf.wechat.domain.InMagEntity;
import cn.wolf.wechat.domain.OutMagEntity;
import cn.wolf.wechat.util.AccessTokenUtil;
import cn.wolf.wechat.util.TokenUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.xml.bind.annotation.XmlElementWrapper;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Date;


@RestController
public class WeChatController {

    @GetMapping("lbw")
    public Object createInterface() throws UnsupportedEncodingException {
        AccessTokenUtil.createInterface();
        return "哈哈哈哈";
    }

    @GetMapping("weChatTest")
    public Object weChatTest(String signature,String timestamp,String nonce,String echostr){
        String[] arr = {TokenUtil.CONFIG_TOKE,timestamp,nonce};
        Arrays.sort(arr);
        StringBuilder sb = new StringBuilder();
        for (String a : arr) {
            sb.append(a);
        }
        String mySignature = SecureUtil.sha1(sb.toString());
        if(!mySignature.equals(signature)){
            return null;
        }
        return echostr;
    }

    @PostMapping(value = "weChatTest",produces = MediaType.TEXT_XML_VALUE)
    public Object ResponseMsg(@RequestBody InMagEntity msg){
        System.out.println(msg);
        OutMagEntity outMag = new OutMagEntity();
        // 发送方
        outMag.setToUserName(msg.getFromUserName());
        // 接受方
        outMag.setFromUserName(msg.getToUserName());
        // 创建消息时间
        outMag.setCreateTime(new Date().getTime());
        // 判断msg中的消息类型是文本还是图片
        if("text".equals(msg.getMsgType())){
            // 消息类型
            outMag.setMsgType("text");
            // 回复内容
            outMag.setContent(msg.getContent());
        }else if("image".equals(msg.getMsgType())){
            // 消息类型
            outMag.setMsgType("image");
            // 回复内容
            outMag.setMediaId(new String[]{msg.getMediaId()});
        }else if("event".equals(msg.getMsgType())){
            if("subscribe".equals(msg.getEvent())) {
                // 消息类型
                outMag.setMsgType("text");
                // 回复内容
                outMag.setContent("你关注你🐎呢!");
            }
            if("CLICK".equals(msg.getEvent())){
                String eventKey = msg.getEventKey();
                if("Advisory".equals(eventKey)){
                    // 消息类型
                    outMag.setMsgType("text");
                    // 回复内容
                    outMag.setContent("我们公众号已经和百度协商合作了，现在有什么问题可以不用来问我了可以直接去问百度😀");
                }
                if("PleaseMoney".equals(eventKey)){
                    // 消息类型
                    outMag.setMsgType("image");
                    // 回复内容
                    outMag.setMediaId(new String[]{"zq9JKfWnfzDI5mp3WHiuvvz_LrXcg9Xjc63MQf6m43Ip6odzusnWvBIdqnD7P-91"});
                }
            }
        }
        RestTemplate restTemplate = new RestTemplate();
        return outMag;
    }
}
